package com.naiterui.ehp.bs.patient.controller;

import com.naiterui.common.repository.page.PageParamVO;
import com.naiterui.common.repository.page.PageVO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.context.ResourceHolder;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.security.SettingConfig;
import com.naiterui.ehp.bs.doctor.vo.followup.AdCalendarPointVO;
import com.naiterui.ehp.bs.doctor.vo.followup.AdVisitVO;
import com.naiterui.ehp.bs.doctor.vo.followup.DrFollowUpCheckVO;
import com.naiterui.ehp.bs.doctor.vo.followup.DrFollowUpRecordFormVO;
import com.naiterui.ehp.bs.doctor.vo.followup.DrFollowUpRecordVO;
import com.naiterui.ehp.bs.doctor.vo.followup.DrFollowUpSubVisitVO;
import com.naiterui.ehp.bs.patient.service.IApFollowUpRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * 随访任务
 *
 * @author yangfuya
 * @since 2022/1/7
 */
@Slf4j
@Api(tags = {SwaggerTag.PATIENT + SwaggerTag.FOLLOW_UP})
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("ap/followup")
public class ApFollowUpRecordController {
    private final IApFollowUpRecordService followUpRecordService;


    @GetMapping
    @ApiOperation(value = "随访管理列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "doctorId", value = "患者Id", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态 1.进行中 2.已结束 ", required = true, dataType = "int",
                    paramType = "query")})
    public List<DrFollowUpRecordVO> list(Long patientId, @NotNull(message = "状态不能为空") @RequestParam Integer status)
            throws BusinessException {
        if (null == patientId) {
            patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        }
        return this.followUpRecordService.list(patientId, status);
    }

    @GetMapping("/detail")
    @ApiOperation(value = "患者端获取单个随访详情（包含复诊和问卷详情）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "随访计划id", required = true, dataType = "long", paramType = "query")
    })
    public DrFollowUpRecordVO getById(@RequestParam Long id) throws BusinessException {
        return this.followUpRecordService.getById(id);
    }

    @GetMapping("/subsequent/visit/detail")
    @ApiOperation(value = "患者端获取包含多次复诊的计划详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "followNo", value = "随访复诊No", required = true, dataType = "String", paramType = "query")
    })
    public List<DrFollowUpSubVisitVO> getById(@RequestParam String followNo) throws BusinessException {
        return this.followUpRecordService.getSubsequentVisitByFollowNo(followNo);
    }

    @GetMapping("/switchForm")
    @ApiOperation(value = "根据随访表单id切换表单")
    @ApiImplicitParam(name = "followUpRecordFormId", value = "随访任务表单id", required = true, dataType = "long",
            paramType = "query")
    public DrFollowUpRecordFormVO switchForm(@RequestParam Long followUpRecordFormId) throws BusinessException {
        return this.followUpRecordService.switchForm(followUpRecordFormId);
    }

    @PostMapping("/form/data/{followUpRecordFormId}")
    @ApiOperation(value = "保存表单数据")
    @ApiImplicitParam(name = "followUpRecordFormId", value = "随访表单id", required = true, dataType = "long",
            paramType = "path")
    public void saveData(@PathVariable Long followUpRecordFormId, @RequestBody Map<String, Object> data)
            throws BusinessException {
        log.info("保存表单数据 请求参数：followUpRecordFormId = {},data = {}", followUpRecordFormId, data);
        this.followUpRecordService.saveData(followUpRecordFormId, data);
    }

    @PostMapping("/cache/save/{followUpRecordFormId}")
    @ApiOperation(value = "保存草稿箱")
    @ApiImplicitParam(name = "followUpRecordFormId", value = "随访表单id", required = true, dataType = "long",
            paramType = "path")
    public void cacheSave(@PathVariable Long followUpRecordFormId, @RequestBody Map<String, Object> data)
            throws BusinessException {
        log.info("保存草稿箱 请求参数：followUpRecordFormId = {},data = {}", followUpRecordFormId, data);
        this.followUpRecordService.cacheSave(followUpRecordFormId, data);
    }

    @GetMapping("/cache/get/{followUpRecordFormId}")
    @ApiOperation(value = "获取草稿箱")
    @ApiImplicitParam(name = "followUpRecordFormId", value = "随访表单id", required = true, dataType = "long",
            paramType = "path")
    public Map<String, Object> cacheGet(@PathVariable Long followUpRecordFormId) throws BusinessException {
        log.info("获取草稿箱 请求参数：followUpRecordFormId = {}", followUpRecordFormId);
        return this.followUpRecordService.cacheGet(followUpRecordFormId);
    }

    @PostMapping("/upload")
    @ApiOperation(value = "上传文件")
    public String upload(MultipartFile file) throws BusinessException {
        return this.followUpRecordService.upload(file);
    }

    @GetMapping("/check")
    @ApiOperation(value = "检查随访")
    public DrFollowUpCheckVO check(@RequestParam Long id) throws BusinessException {
        return this.followUpRecordService.checkFollowUp(id);
    }

    @GetMapping("today")
    @ApiOperation(value = "患者某天的随访计划", notes = "患者某天的随访计划")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "date", value = "具体日期yyyy-MM-dd", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "patientId", value = "患者id", required = true, dataType = "long", paramType = "query")
    })
    public PageVO<AdVisitVO> todayList(@NotNull(message = "患者ID不能为null") Long patientId, String date, PageParamVO pageParamVO) throws BusinessException {
        log.info("患者某天的随访计划 请求参数：patientId = {}, date = {}, pageParamVO = {} ", patientId, date, pageParamVO);
        return this.followUpRecordService.getVisitTodayPage(patientId, date, pageParamVO);
    }

    @GetMapping("month")
    @ApiOperation(value = "患者某月的随访计划日期展示", notes = "患者某月的随访计划日期展示")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "month", value = "月份 yyyy-MM ", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "patientId", value = "患者id", required = true, dataType = "long", paramType = "query")
    })
    public List<AdCalendarPointVO> getMonthVisit(@NotNull(message = "患者ID不能为null") Long patientId,
                                                 @NotEmpty(message = "月份不能为null") String month) throws BusinessException {
        log.info("患者某月的随访计划日期展示 请求参数：patientId = {}, month = {}", patientId, month);
        return this.followUpRecordService.getMonthVisit(patientId, month);
    }
}
